import libuvk5
import sys
import os


# Handle arguments
if len(sys.argv) not in [2,3]: print(f'Usage: {os.path.basename(sys.argv[0])} <COMx> <address> <hex_payload>') ; exit(1)

arg_port = sys.argv[1]

# Defaults taken from my radio
# To get from RSSI to the byte value, use the following formula:
# byte_value = (rssi + 160) / 2
uhf_squelch_open_rssi = bytearray([0x0a,0x4b,0x53,0x56,0x59,0x5c,0x5f,0x62,0x64,0x66,0xff,0xff,0xff,0xff,0xff,0xff])
uhf_squelch_close_rssi = bytearray([0x05,0x46,0x50,0x53,0x56,0x59,0x5c,0x5f,0x62,0x64,0xff,0xff,0xff,0xff,0xff,0xff])
uhf_squelch_open_noise = bytearray([0x5a,0x2d,0x29,0x26,0x23,0x20,0x1d,0x1a,0x17,0x14,0xff,0xff,0xff,0xff,0xff,0xff])
uhf_squelch_close_noise = bytearray([0x64,0x30,0x2d,0x29,0x26,0x23,0x20,0x1d,0x1a,0x17,0xff,0xff,0xff,0xff,0xff,0xff])
uhf_squelch_close_glitch = bytearray([0x5a,0x14,0x11,0x0e,0x0b,0x08,0x03,0x02,0x02,0x02,0xff,0xff,0xff,0xff,0xff,0xff])
uhf_squelch_open_glitch = bytearray([0x64,0x11,0x0e,0x0b,0x08,0x05,0x05,0x04,0x04,0x04,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_open_rssi = bytearray([0x32,0x68,0x6b,0x6e,0x6f,0x72,0x75,0x77,0x79,0x7b,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_close_rssi = bytearray([0x28,0x64,0x67,0x6a,0x6c,0x6e,0x71,0x73,0x76,0x78,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_open_noise = bytearray([0x41,0x32,0x2d,0x28,0x24,0x21,0x1e,0x1a,0x17,0x16,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_close_noise = bytearray([0x46,0x37,0x32,0x2d,0x28,0x25,0x22,0x1e,0x1b,0x19,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_close_glitch = bytearray([0x5a,0x19,0x0f,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0xff,0xff,0xff,0xff,0xff,0xff])
vhf_squelch_open_glitch = bytearray([0x64,0x1e,0x14,0x0f,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0xff,0xff,0xff,0xff,0xff,0xff])



# Connect and read
with libuvk5.uvk5(arg_port) as radio:
    if radio.connect():
        _=radio.get_fw_version() #mandatory before reading mem
        radio.set_cfg_mem(0x1e00,uhf_squelch_open_rssi).hex()
        radio.set_cfg_mem(0x1e10,uhf_squelch_close_rssi).hex()
        radio.set_cfg_mem(0x1e20,uhf_squelch_open_noise).hex()
        radio.set_cfg_mem(0x1e30,uhf_squelch_close_noise).hex()
        radio.set_cfg_mem(0x1e40,uhf_squelch_close_glitch).hex()
        radio.set_cfg_mem(0x1e50,uhf_squelch_open_glitch).hex()
        radio.set_cfg_mem(0x1e60,vhf_squelch_open_rssi).hex()
        radio.set_cfg_mem(0x1e70,vhf_squelch_close_rssi).hex()
        radio.set_cfg_mem(0x1e80,vhf_squelch_open_noise).hex()
        radio.set_cfg_mem(0x1e90,vhf_squelch_close_noise).hex()
        radio.set_cfg_mem(0x1ea0,vhf_squelch_close_glitch).hex()
        radio.set_cfg_mem(0x1eb0,vhf_squelch_open_glitch).hex()
        